home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: How to do a 32bit x 32bit multiplication
- Date: 27 Feb 1996 08:52:58 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gvctaINNe23@anvil.ugrad.cs.ubc.ca>
- References: <DnA53F.KnG.0.-s@hkusuc.hku.hk>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <DnA53F.KnG.0.-s@hkusuc.hku.hk>,
- Chow Cheuk Hung <h9316216@hkueee2.eee.hku.hk> wrote:
-
- Subject: How to do 32bit x 32bi multiplication
-
-
- The answer: use a 32-bit CPU.
-
- Now, if the question was "how to do 32bit x 32bit multiplication, _and_ keep
- the full 64 bit result", I'd say:
-
- The standard gives you a guaranteed ``minimum maximum'' size for an unsigned
- long integer of 2^32-1. This means that you can do 16x16->32 multiplications
- two unsigned shorts to get a result that can always fit into the unsigned long
- type.
-
- Using such multiplications, you can build up a function or macro that does
- unsigned 32x32->64 multiplication, though it won't be very fast.
-
- Another (non portable) way is to see if your compiler supports an extra
- precision long. For example, the GCC compiler has a "long long" type which
- holds 64 bits. This is convenient to work with, at the price of breaking with
- standard compliance.
-
- To get a long long result from a multiplication, you have to cast both operands
- to long long, like this:
-
- long a = 3, b = 4;
- long long b = (long long) a * (long long) b;
-
- There is no automatic promotion from long to long long, hence the casts.
- Without the cast, the result will be chopped to 32 bits before being assigned
- to b.
-
-
- --
-
-